package day15.day15_2.string;

import org.junit.Test;

import java.util.Arrays;

/**
 * @Auther:小陈的Java
 */
public class StringDemo2 {
    public void test1(){
        System.out.println("abc".equals("abc"));
        //忽略大小写比较字符串内容是否相等
        System.out.println("abc".equalsIgnoreCase("ABC"));
        //比较字母的字典顺序
        System.out.println("a".compareTo("b"));
        //判断字符串是否以"a"开头
        System.out.println("abc".startsWith("a"));
        //测试此字符串从指定索引开始的子字符串是否以指定前缀开始
        System.out.println("abc".startsWith("b",1));
        //判断字符串是否以bc结尾
        System.out.println("abc".endsWith("bc"));
        //判断字符串是否包含"b"
        System.out.println("abc".contains("b"));
    }
    @Test
    public void test2() throws Exception{
        //字符串转大写
        System.out.println("abc".toUpperCase());
        //字符串转小写
        System.out.println("ABC".toLowerCase());
        //转byte
        byte[] bytes = "我爱你中国".getBytes();
        System.out.println(Arrays.toString(bytes));
        byte[] bytes2 = "我爱你中国".getBytes("gbk");
        System.out.println(Arrays.toString(bytes2));
        //转char数组
        char[] chars = "我爱你中国".toCharArray();
        System.out.println(Arrays.toString(chars));
    }
    @Test
    public void test3(){
        String s="今天天气好晴朗";
        System.out.println(s.substring(2));//天气好晴朗
        System.out.println(s.substring(2,3));//天
        System.out.println(s.indexOf("好"));//4
        System.out.println(s.indexOf("好",1));//4
        System.out.println(s.lastIndexOf("好"));//4
        System.out.println(s.lastIndexOf("好",3));//-1
    }
    @Test
    public  void test4(){
        String s="01234567891234";
        System.out.println(s.replace('7','a'));//0123456a891234
        System.out.println(s.replace("123","ABC"));//0ABC456789ABC4
        System.out.println(s.replaceAll("[1-7]+","666"));//066689666
        System.out.println(s.replaceFirst("[1-4]+","j"));//0j567891234
    }
    @Test
    public void test5(){
        String s= "abcdefgh";
        String e ="";
        String q="窗前明月光，疑是地上霜，举头望明月，低头思故乡";
        System.out.println(s.length());//8
        System.out.println(s.charAt(3));//d
        System.out.println(e.isEmpty());//true
        System.out.println(s.matches("[a-z]+"));//true
        String[] ss= q.split("，",3);
        System.out.println(Arrays.toString(ss));//[窗前明月光, 疑是地上霜, 举头望明月，低头思故乡]
    }
    @Test
    public void test6(){
        String str = "abc";
        String result = str.toUpperCase();
        System.out.println(result);
        System.out.println(str);
        System.out.println(str.substring(1));
        System.out.println(str);
        String result2 = str.replace('c', 'C');
        System.out.println(result2);
        System.out.println(str);
    }
}
